// pages/submit/submit.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    age:0,
    date:"",
    time:"",
    locationName:"",
    locationAddress:"",
    phoneNum:"",
    pic:[],
    latitude:"",
    longitude:"",
    countNum:0,
    sex:"",
    orderNoteMax:200,
    beizhu:"",
    picUrlAll:""
  },
  //获取男女
  getSex(e){
    // console.log(e)
    var sex=""
    if(e.detail.value == "r1"){
      sex = "1"
    }else{
      sex = "0"
    }
    this.setData({
      sex:sex
    })
  },
  // 获取日期
  bindDateChange: function (e) {
    this.setData({
        date: e.detail.value
    })
  },
  // 获取详细时间
  bindTimeChange: function (e) {
    this.setData({
        time: e.detail.value
    })
  },
  getPic(e){
    wx.chooseImage({
      count: 5,
      success:res=>{
        wx.showLoading({
          title: '图片上传中 ',
        })
        var pic = ''
        var piclen = res.tempFilePaths.length
        pic = res.tempFilePaths
        console.log(res)
        this.setData({
          pic:pic
        })
        var picUrlAll=''
       for(var index in pic){

          wx.uploadFile({
            filePath: pic[index],
            name:'file',
            header: {
              "Content-Type": "multipart/form-data",
              Authorization:app.globalData.header
             },
        
            url: app.globalData.Baseip+":8881/file/upload?fileName="+pic[index],
            success:res=>{
              var url = JSON.parse(res.data).data
              // console.log(url)
              picUrlAll=picUrlAll+url+','
              // console.log(index,piclen)

              if(index == piclen-1){
                this.setData({
                  picUrlAll:picUrlAll
                })
                console.log(picUrlAll)
                wx.hideLoading()
              }
            },
            fail:err=>{
              console.log(err)
            }
          })
          
         
        }
      
       
       
      },
      fail:err=>{

      }
    })

  },
  //订单备注textarea
  inputs: function (e) {
    // 获取输入框的内容
    var value = e.detail.value;
    this.setData({//更新备注内容到vue缓存
      beizhu: e.detail.value
    })
    // 获取输入框内容的长度
    var len = parseInt(value.length);

 
    //最多字数限制
    if(len > this.data.orderNoteMax) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
    this.setData({
      countNum: len //当前字数
    });
  },

  //获取地址
  getLoction(){
    wx.chooseLocation({
      // type:"gcj02",
      success:(res=>{
        console.log(res)
        this.setData({
          locationName:res.name,
          locationAddress:res.address,
          latitude:res.latitude,
          longitude:res.longitude
        })
      }),
      fail:(err=>{
        console.log(err)
      })
    })
    
  },

  // 获取表单信息 并 提交
  submitInfo(e){
    // console.log(e)
    this.setData({
      phoneNum:e.detail.value.phoneNum,
      name:e.detail.value.name,
      age:e.detail.value.age
    })
   

   console.log(this.data.date + ' ' + this.data.time)
    wx.request({
      url: app.globalData.Baseip+':8881/situation/releaseForHelp',
      data:{
        "name":this.data.name,
        "lostTime":this.data.date + ' ' + this.data.time+':00',
        "sex":this.data.sex,
        "age":this.data.age,
        "lostAddress":this.data.locationName,
        "phoneNum":this.data.phoneNum,
        "lastFindLatitude":this.data.latitude,
        "lastFindLongitude":this.data.longitude,
        "album":this.data.picUrlAll,
        "other":this.data.beizhu
      },
      method:'POST',
      header: {
       Authorization:app.globalData.header
      },
      success:function(res){
        console.log(res)
        wx.navigateBack({
          delta: 1,
        })
      },
      fail:function(err){
        console.log(err)
      }
    })
  


  },
  reset(){
    this.setData({
      name:"",
      age:0,
      date:"",
      time:"",
      locationName:"",
      locationAddress:"",
      phoneNum:"",
      pic:[],
      latitude:"",
      longitude:"",
      countNum:0,
      sex:"",
      orderNoteMax:200
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})